package main

import "fmt"

//这样写的优势参看 练习/06求数组所有元素之和
func sumArray(a []int) int {
  var sum int = 0
  for _, v := range a {
    sum = sum + v
  }
  return sum
}

func modifySlice(a []int) {
  a[0] = 1000
}


func testSumArray() {
  var a [10]int = [10]int{1, 2, 3, 4, 5, 6, 7, 8, 9}
  sum := sumArray(a[:])
  fmt.Println("sum:", sum) //sum: 45

  var b [3]int = [3]int{10, 20, 30}
  sum = sumArray(b[:])
  fmt.Println("sum:", sum) //sum: 60
}

//GO里切片是引用类型(数组不是)
func testModifySlice() {
  var a[3]int = [3]int{1,2,3}
  modifySlice(a[:])
  fmt.Println(a)
/*
   [1000 2 3]
*/
}

func testModifyCopy(){
  //var a[]int = []int{1,2,3}
  //var b[]int = []int{4,5,6}
  //copy(a,b) //把b拷贝到a
  //fmt.Println("a:",a) //a: [4 5 6]
  //fmt.Println("b:",b) //a: [4 5 6]


  //不会进行扩容
  var a[]int = []int{1,2}
  var b[]int = []int{4,5,6}
  copy(a,b) //把b拷贝到a
  fmt.Println("a:",a) //a: [4 5]
  fmt.Println("b:",b) //a: [4 5 6]
}

func main() {
  //testSumArray()

  //testModifySlice()

  testModifyCopy()
}
